var Permissions = {};

Permissions.listOrigins = [];
Permissions.listPermissions = [];

Permissions.init = function()
{
    chrome.permissions.getAll(Permissions.onGetAllCallback);
    chrome.permissions.onAdded.addListener(Permissions.onAdd);
    chrome.permissions.onRemoved.addListener(Permissions.onRemove);
};

Permissions.removeOrigin = function(origin)
{
  chrome.permissions.remove({origins: [origin]});
  Permissions.onRemove({origins: [origin]});
}

Permissions.onAdd = function(permissions)
{
    if (permissions.origins)
        Permissions.listOrigins.concatUniq$(permissions.origins)
    if (permissions.permissions)
        Permissions.listPermissions.concatUniq$(permissions.permissions)
};

Permissions.onRemove = function(permissions)
{
    if (permissions.origins)
    {
        for (var i = 0; i < permissions.origins.length; i++)
        {
            var perm = permissions.origins[i];
            var index = Permissions.listOrigins.indexOf(perm);
            if (index)
                Permissions.listOrigins.splice(index,1);
        }

    }
    if (permissions.permissions)
    {
        for (var i = 0; i < permissions.permissions.length; i++)
        {
            var perm = permissions.permissions[i];
            var index = Permissions.listPermissions.indexOf(perm);
            if (index)
                Permissions.listPermissions.splice(index,1);
        }

    }

};

Permissions.onGetAllCallback = function(permissions)
{
    if (permissions.origins)
        Permissions.listOrigins.concatUniq$(permissions.origins)
    if (permissions.permissions)
        Permissions.listPermissions.concatUniq$(permissions.permissions)
};


Permissions.hasOrigin = function(origin)
{
    var ret = Permissions.listOrigins.indexOf(origin);
    if (ret == null)
        return false;
    else
        return true;
};

Permissions.hasPermission = function(permission)
{
    var ret = Permissions.listPermissions.indexOf(permission);
    if (ret == null)
        return false;
    else
        return true;
};